package codeRandomThoughts.Test349两个数组的交集;

import java.util.HashSet;
import java.util.Set;

//二刷
//0 <= nums1[i], nums2[i] <= 1000
//输出结果无重复
public class Solution2 {
    public int[] intersection(int[] nums1, int[] nums2) {
        int[] map = new int[1001];
        for (int i : nums1) {
            map[i]++;
        }
        Set<Integer> res = new HashSet<>();
        for (int i : nums2) {
            if (map[i] > 0) {
                res.add(i);
                map[i]--;
            }
        }

        int[] arr = res.stream().mapToInt(k -> k).toArray();
        return arr;
    }
}
